<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : XUpdateStylesheet.xsl
    Created on : 17. červen 2011, 23:09
    Author     : Lukáš Ručka
    Description: Transform input kml subset to XUpdate query
-->

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.opengis.net/kml/2.2"
    xmlns:kml2="http://www.opengis.net/kml/2.2"
    xmlns:xupdate="http://www.xmldb.org/xupdate"
    version="1.0">


    <xsl:output method="xml"/>

    <!--
        XSLT 1.0 supposingly has some problems with default namespace, this duplicity should hack around it
    -->

    <xsl:template match="/kml2:kml">
        <xupdate:modifications version="1.0" xmlns="http://www.opengis.net/kml/2.2" xmlns:xupdate="http://www.xmldb.org/xupdate">
            <xsl:apply-templates select="kml2:Placemark" />
        </xupdate:modifications>

<!--    this is previous code that had to be replaced by above-mentioned code
        <xsl:element name="xupdate:modifications">
            <xsl:attribute name="version">1.0</xsl:attribute>
            <xsl:apply-templates select="kml2:Placemark" />
        </xsl:element>
-->
    </xsl:template>
    <xsl:template match="/kml">
        <xupdate:modifications version="1.0" xmlns="http://www.opengis.net/kml/2.2" xmlns:xupdate="http://www.xmldb.org/xupdate">
            <xsl:apply-templates select="Placemark" />
        </xupdate:modifications>
<!--
        <xsl:element name="xupdate:modifications">
            <xsl:attribute name="version">1.0</xsl:attribute>
            <xsl:apply-templates select="Placemark" />
        </xsl:element>
-->
    </xsl:template>

    <xsl:template match="/kml/Placemark">
        <xsl:element name="xupdate:append">
            <xsl:attribute name="select">/kml2:kml/kml2:Document</xsl:attribute>
            <xsl:attribute name="child">last()</xsl:attribute>
            <Placemark>
                <name><xsl:value-of select="name/text()" /></name>
                <Snippet max-lines="1"><xsl:value-of select="Snippet/text()" /></Snippet>
                <description><xsl:value-of select="description/text()" /></description>
                <Point><coordinates><xsl:value-of select="normalize-space(Point/coordinates/text())"/></coordinates></Point>
            </Placemark>
        </xsl:element>
    </xsl:template>

    <xsl:template match="/kml2:kml/kml2:Placemark">
        <xsl:element name="xupdate:append">
            <xsl:attribute name="select">/kml2:kml/kml2:Document</xsl:attribute>
            <xsl:attribute name="child">last()</xsl:attribute>
            <Placemark>
                <name><xsl:value-of select="kml2:name/text()" /></name>
                <Snippet max-lines="1"><xsl:value-of select="kml2:Snippet/text()" /></Snippet>
                <description><xsl:value-of select="kml2:description/text()" /></description>
                <Point><coordinates><xsl:value-of select="normalize-space(kml2:Point/kml2:coordinates/text())"/></coordinates></Point>
            </Placemark>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
